import {reactive, ref, toRaw} from "vue";
import type IPageResp from "@/type/IPageResp.ts";
import resetDefault from "@/util/reset-default.ts";
import type IPageReq from "@/type/IPageReq.ts";
import type ISysDictReq from "./type/req/ISysDictReq.ts";
import type ISysDictResp from "./type/resp/ISysDictResp.ts";
import sysDictApi from "./api/SysDictApi.ts";

/**
 * 字典表的CRUD相关代码
 */
export const useSysDictCrud = () => {

    /**
     * 分页数据
     */
    const pageModel = reactive<IPageReq>({
        pageNo: 1,
        pageSize: 10
    })

    /**
     * search表单数据
     */
    const searchModel = reactive<ISysDictReq>({

    })

    /**
     * 表格数据
     */
    const tableModel = reactive<IPageResp<ISysDictResp>>({
        current: 1,
        size: 10,
        pages: 0,
        total: 0,
        records: []
    })

    /**
     * 新增和编辑框数据
     */
    const dialogModel = reactive<ISysDictReq>({
    })

    /**
     * 分页查询
     */
    let loading = ref<boolean>(false)

    const page = () => {
        loading.value = true
        sysDictApi.pageDict({...toRaw(searchModel), ...toRaw(pageModel)})
            .then(result => {
                tableModel.current = result.current
                tableModel.size = result.size
                tableModel.pages = result.pages
                tableModel.total = result.total
                tableModel.records = result.records
            })
            .catch(() => {
                //查询出错，就清空数据
                tableModel.records = []
            })
            .finally(() => {
                loading.value = false
            })
    }

    const reset = () => {
        resetDefault(searchModel)
        page()
    }

    const pageChange = (pageNo: number, pageSize: number) => {
        pageModel.pageNo = pageNo
        pageModel.pageSize = pageSize
        page()
    }

    /**
     * 字典项的drawer
     */
    let drawerVisible = ref(false)

    let currentDictCode: string | undefined

    const dictItems = ref<ISysDictResp[]>([])

    const openDrawer = async (dictCode: string) => {
        currentDictCode = dictCode
        drawerVisible.value = true
        dictItems.value = await sysDictApi.listDictItem({dictCode})
    }

    const closeDrawer = () => {
        currentDictCode = undefined
        drawerVisible.value = false
    }

    /**
    * 新增和编辑的dialog
    */
    let dialogVisible = ref(false)
    const openDialog = (row: any) => {
        if (row) {
            Object.assign(dialogModel, row)
        }
        dialogVisible.value = true
    }
                    
    const closeDialog = () => {
        resetDefault(dialogModel)
        dialogVisible.value = false
    }

    /**
     * 字典项item新增和编辑的dialog
     */
    let itemDialogVisible = ref(false)
    const itemOpenDialog = (row: any = undefined) => {
        if (row) {
            Object.assign(dialogModel, row)
        }
        dialogModel.dictCode = currentDictCode
        dialogModel.type = 1
        itemDialogVisible.value = true
    }

    const itemCloseDialog = () => {
        resetDefault(dialogModel)
        itemDialogVisible.value = false
    }

    /**
     * 新增或编辑
     */
    const addOrUpdate = async () => {
        await sysDictApi.saveOrUpdateDict(dialogModel)
        closeDialog()
        page()
    }

    const saveOrUpdateDictItem = async () => {
        await sysDictApi.saveOrUpdateDictItem(dialogModel)
        itemCloseDialog()
        dictItems.value = await sysDictApi.listDictItem({dictCode: currentDictCode})
    }

    /**
    * 删除
    */
    const deleteByIds = async (ids: string[]) => {
        await sysDictApi.deleteDict(ids)
        page()
    }

    const deleteDictItem = async (ids: string[]) => {
        await sysDictApi.deleteDictItem(ids)
        dictItems.value = await sysDictApi.listDictItem({dictCode: currentDictCode})
    }

    return {
        searchModel,
        tableModel,
        dialogModel,
        loading,
        dictItems,
        currentDictCode,
        drawerVisible,
        dialogVisible,
        itemDialogVisible,
        page,
        reset,
        pageChange,
        openDrawer,
        closeDrawer,
        itemOpenDialog,
        itemCloseDialog,
        openDialog,
        closeDialog,
        addOrUpdate,
        saveOrUpdateDictItem,
        deleteByIds,
        deleteDictItem
    }
}